/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 * Description: 获取动态库中qos_manager接口函数的适配接口
 * Author: lanzeze
 * Create: 2023-06-27
 */

#include <string>
#include <map>
#include "log.h"
#include "hccl_dl.h"
#include "dlqos_function.h"

namespace hccl {
DlQosFunction &DlQosFunction::GetInstance()
{
    static DlQosFunction hcclDlHalFunction;
    return hcclDlHalFunction;
}

DlQosFunction::DlQosFunction() : handle_(nullptr)
{
}

DlQosFunction::~DlQosFunction()
{
    if (handle_ != nullptr) {
        (void)HcclDlclose(handle_);
        handle_ = nullptr;
    }
}

HcclResult DlQosFunction::DlQosFunctionEschedInit()
{
    dlGetStreamEngineQos = (QosErrorCode(*)(QosStreamType label, QosEngineType engine,
        const std::string &op, int devId, QosConfig* info))
        HcclDlsym(handle_, "QosGetStreamEngineQos");
    CHK_SMART_PTR_NULL(dlGetStreamEngineQos);

    return HCCL_SUCCESS;
}

HcclResult DlQosFunction::DlQosFunctionInit()
{
    std::lock_guard<std::mutex> lock(handleMutex_);
    if (handle_ == nullptr) {
        handle_ = HcclDlopen("libqos_manager.so", RTLD_NOW);
        const char* errMsg = dlerror();
        CHK_PRT_RET(handle_ == nullptr, HCCL_ERROR("dlopen [%s] failed, %s", "libqos_manager.so",\
            (errMsg == nullptr) ? "please check the file exist or permission denied." : errMsg),\
            HCCL_E_OPEN_FILE_FAILURE);
    }

    CHK_RET(DlQosFunctionEschedInit());
    return HCCL_SUCCESS;
}
}
